package com.sgcc.fj.pmos.mobileapp.fragment;

import android.graphics.Color;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lxj.xpopup.interfaces.OnConfirmListener;
import com.sgcc.fj.pmos.arouter.app.AppArouterPath;
import com.sgcc.fj.pmos.common.base.MvpBaseRecycleFragment;
import com.sgcc.fj.pmos.common.bean.EventCenter;
import com.sgcc.fj.pmos.common.utils.ArguConstant;
import com.sgcc.fj.pmos.common.utils.DialogUtil;
import com.sgcc.pmos.nativesdk.R;
import com.sgcc.fj.pmos.mobileapp.adapter.ContactPersonInfoAdapter;
import com.sgcc.fj.pmos.mobileapp.bean.LinkmanInfoBean;
import com.sgcc.fj.pmos.mobileapp.contract.ContactPersonInfoContract;
import com.sgcc.fj.pmos.mobileapp.presenter.ContactPersonInfoPresenter;


/**
 * @author ChaoAJ
 * @describe 关联企业信息--联系人信息
 * @time 2022/10/18 11:56
 */
public class ContactPersonInfoFragment extends MvpBaseRecycleFragment<LinkmanInfoBean.DataBean, ContactPersonInfoPresenter> implements ContactPersonInfoContract.View {


    private TextView mBtnAddContact;

    private ContactPersonInfoAdapter mContactPersonInfoAdapter;


    public static ContactPersonInfoFragment newInstance() {
        ContactPersonInfoFragment fragment = new ContactPersonInfoFragment();
        return fragment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        recycleview.setBackgroundColor(Color.parseColor("#ffffff"));
        refreshLayout.setEnableLoadMore(false);
    }

    @Override
    protected ContactPersonInfoPresenter createPresenter() {
        return new ContactPersonInfoPresenter();
    }

    @Override
    public View getHeaderLayout(RelativeLayout parent) {
        View head_view = getLayoutInflater().inflate(R.layout.header_contactpersoninfo_layout, parent, false);
        return head_view;
    }

    private void findViewByIds(View footer_view) {
        mBtnAddContact = (TextView) footer_view.findViewById(R.id.btn_add_contact);
        mBtnAddContact.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                if (null == mAddOrEditContactPopup) {
//                    mAddOrEditContactPopup = (AddOrEditContactPopup) new XPopup.Builder(mContext)
//                            .maxWidth((int) (XPopupUtils.getScreenWidth(getContext()) - 50))
//                            .asCustom(new AddOrEditContactPopup(mContext, new AddOrEditContactPopup.OnCallBackListener() {
//                                @Override
//                                public void save() {
//                                    refresh(false);
//                                }
//                            }, null))
//                            .show();
//                } else {
//                    mAddOrEditContactPopup.show();
//                }


                ARouter.getInstance()
                        .build(AppArouterPath.APP_ADDOREDITCONTACT)
                        .withBoolean("isHasCommomContact",isHasCommomContact())
                        .navigation();
            }
        });
    }

//    private AddOrEditContactPopup mAddOrEditContactPopup;

    @Override
    public boolean isBindEventBusHere() {
        return true;
    }

    @Override
    public void onEventMainThread(EventCenter eventCenter) {
        super.onEventMainThread(eventCenter);
        if (eventCenter.getEventCode() == ArguConstant.SAVE_CONTACT_BACK) {
//            if (null != mAddOrEditContactPopup) {
//                FileInfo bean = (FileInfo) eventCenter.getData();
//                mAddOrEditContactPopup.setText(bean);
//            }

            refresh(false);
        }
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        mContactPersonInfoAdapter = new ContactPersonInfoAdapter();
        View footer_view = getLayoutInflater().inflate(R.layout.footer_contactpersoninfo_layout, recycleview, false);
        findViewByIds(footer_view);
        mContactPersonInfoAdapter.addFooterView(footer_view);
        mContactPersonInfoAdapter.setBtnClickListener(new ContactPersonInfoAdapter.BtnClickListener() {
            @Override
            public void delete(int position, LinkmanInfoBean.DataBean bean) {

                DialogUtil.showConfirm(getContext(), "提示", "是否删除联系人？", new OnConfirmListener() {
                    @Override
                    public void onConfirm() {
                        mPresenter.deleteLinkmanInfo("123456", bean.getGuid());
                    }
                });


            }

            @Override
            public void edit(int position, LinkmanInfoBean.DataBean bean) {
//                if (null == mAddOrEditContactPopup) {
//                    mAddOrEditContactPopup = (AddOrEditContactPopup) new XPopup.Builder(mContext)
//                            .maxWidth((int) (XPopupUtils.getScreenWidth(getContext()) - 50))
//                            .asCustom(new AddOrEditContactPopup(mContext, new AddOrEditContactPopup.OnCallBackListener() {
//                                @Override
//                                public void save() {
//                                    refresh(false);
//                                }
//                            }, bean))
//                            .show();
//                } else {
//                    mAddOrEditContactPopup.show();
//                }


                ARouter.getInstance()
                        .build(AppArouterPath.APP_ADDOREDITCONTACT)
                        .withBoolean("isHasCommomContact",isHasCommomContact())
                        .withSerializable("LinkmanInfoBean", bean)
                        .navigation();
            }
        });
        return mContactPersonInfoAdapter;
    }

    @Override
    public void getDataRequest(int listSize, boolean isloading) {
        super.getDataRequest(listSize, isloading);
        mPresenter.queryLinkmanInfo("123456");
    }

    @Override
    public void queryLinkmanInfoSuccess(LinkmanInfoBean response) {
        loadSuccess(false, response.getData());
    }

    @Override
    public boolean isShowContent() {
        return true;
    }

    @Override
    public void deleteLinkmanInfoSuccess() {
        refresh(false);
    }


    public boolean next() {
        if (!isHasCommomContact()) {
            ToastUtils.showShort("请添加第一联系人");
            return false;
        }
        return true;
    }


    private boolean isHasCommomContact(){
        boolean isHasCommomContact = false;

        for (LinkmanInfoBean.DataBean dataBean : mContactPersonInfoAdapter.getData()) {
            if (dataBean.getIsCommomContact().equals("1")) {
                isHasCommomContact = true;
                break;
            }
        }


        return isHasCommomContact;
    }

}